var location = require("../../../../mybehaviors/location/location");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [location],
  data: {
    active: 2,
    publisherList: [],
    myLocation: {
      address: "",
      city: "",
      point: ''
    },
    classifyData: [],
    publishLists: {
      recommendCategory: [],
      nearby_shifu: [],
      carousel_images: []
    },
    type: "",
    Height: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options && options.type ? options.type : '',
      active:  options && options.type ? options.type * 1 : 2
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   this.ready();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */ 
  onReachBottom() {

  },
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this._getLocation((mapdata,e)=>{
      this.setData({
        'myLocation.address': mapdata.address,
        'myLocation.city': mapdata.address_component.city ? mapdata.address_component.city : mapdata.address_component.province,
        'myLocation.point': e
      });
      this.getClassifyData();
    });
  },
  splitArr (arr,num) {
    var res=[];
    for (var i = 0,len = arr.length; i < len; i+=num) {
      res.push(arr.slice(i, i+num));
    }
    return res
  },
  goMoreList(item) {
   wx.navigateTo({
     url:  `/packageJ/anytime_service/home/publisher_list/index?type=${item.currentTarget.dataset.type}&myLocation=${JSON.stringify(this.data.myLocation)}&id=${item.currentTarget.dataset.id}`,
   });
  },
  // 附近技师更多
  tosearch() {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/home/search/index?name=技师`,
    });
  },
  goividualNeeds(item) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/home/individual_needs/index?type=${item.currentTarget.dataset.type}&order_id=${item.currentTarget.dataset.id}`,
    });
  },
  // 跳转技师师傅主页
  goTechnicianHome(item) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/home/technician_home/index?id=${item.currentTarget.dataset.id}`,
    });
  },
  // 获取分类
  getClassifyData() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.be-within-call.frontend.index.get-category-list"),
      data: {
        sort_type: this.data.active
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          app.tips(res.data.msg);
          return;
        } 
        this.setData({
          classifyData: this.splitArr(res.data,10),
        });
        this.getData();
      },
    });
  },
  splitArr (arr,num) {
    var res=[];
    for (var i = 0,len = arr.length; i < len; i+=num) {
      res.push(arr.slice(i, i+num));
    }
    return res
  },
  getData() {
    let form = {sort_type: this.data.active };
    let url = "plugin.be-within-call.frontend.index.tab-one";
    if(this.data.active == 2) {
      form = {
        lng: this.data.myLocation ? this.data.myLocation.point.lng : "",
        lat: this.data.myLocation ? this.data.myLocation.point.lat : "",
        sort_type: this.data.active
      }
      url = "plugin.be-within-call.frontend.index.tab-two"
    }else if(this.data.active == 3) {
      url = 'plugin.be-within-call.frontend.index.tab-three'
    }
    app._postNetWork({
      url: app.getNetAddresss(url),
      data: form,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          app.tips(res.data.msg);
          return;
        } 
        this.setData({
          ['publishLists.recommendCategory']: res.data.recommendCategory,
          ['publishLists.nearby_shifu']:  this.data.active == 2 ? res.data.nearby_shifu : [],
          ['publishLists.carousel_images']:  res.data.carousel_images
        });
      },
    });
  },
  changeTab(val) {
    this.setData({
      active: val.detail.name
    })
    this.getClassifyData();
  },
  imgHeight: function(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      Height: swiperH //设置高度
    });
  },
})